package com.baway.biliclitent.fragment.ZoneType;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.Toast;

import com.baway.biliclitent.R;
import com.baway.biliclitent.adapter.ZoneLiveFragRecyAdapter;
import com.baway.biliclitent.model.LiveBean;
import com.baway.biliclitent.util.CommonUtil;
import com.baway.biliclitent.util.OkHttpUtils;

/**
 * 分区直播详情页面
 * 王培炎
 */

public class ZoneLiveAppIndexActivity extends AppCompatActivity {

    private Toolbar mToolbar;
    private OkHttpUtils httpUtils;
    private String url = "http://live.bilibili.com/AppIndex/home?_device=android&_hwid=51e96f5f2f54d5f9&_ulv=10000&access_key=563d6046f06289cbdcb472601ce5a761&appkey=c1b107428d337928&build=410000&platform=android&scale=xxhdpi&sign=fbdcfe141853f7e2c84c4d401f6a8758";
    private SwipeRefreshLayout swipeRefresh;
    private RecyclerView recycle;
    private ZoneLiveFragRecyAdapter recyAdfapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.zonelive_activity);
        intiView();
        showBar();
    }
    /**
     *  获取控件id
     */
    private void intiView(){
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        swipeRefresh = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout);
        recycle = (RecyclerView) findViewById(R.id.recycle);
        httpUtils = OkHttpUtils.getHttpUtils();
        if (CommonUtil.isNetworkAvailable(this) ){
            getData(url);
        }else{
            Toast.makeText(this,"当前没有网络连接",Toast.LENGTH_SHORT).show();
        }
        //设置进度条颜色
        swipeRefresh.setColorSchemeResources(R.color.colorPrimary);
        swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //关闭刷新动作
                swipeRefresh.setRefreshing(false);
            }
        });
    }
    /**
     *  获取网络数据
     */
    private void getData(String path) {
        httpUtils.loadDataFromNet(path, LiveBean.class, new OkHttpUtils.CallBackListener<LiveBean>() {
            @Override
            public void onSuccess(LiveBean result) {
                initRecyclerView(result);
            }

            @Override
            public void onFail() {

            }
        });
    }

    /**
     *  适配
     */
    public void initRecyclerView(LiveBean result) {
        //出过整个对象
        recyAdfapter = new ZoneLiveFragRecyAdapter(ZoneLiveAppIndexActivity.this,result);
        //设置管理者进行适配
        GridLayoutManager layout = new GridLayoutManager(this, 12);
        layout.setOrientation(LinearLayoutManager.VERTICAL);
        layout.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {

            @Override
            public int getSpanSize(int position) {

                return recyAdfapter.getSpanSize(position);
            }
        });
        recycle.setLayoutManager(layout);
        recycle.setAdapter(recyAdfapter);
    }
    /**
     *  显示toolBar
     */
    private void showBar(){
        mToolbar.setTitle("直播");
        setSupportActionBar(mToolbar);
        ActionBar supportActionBar = getSupportActionBar();
        if (supportActionBar != null) {
            supportActionBar.setDisplayHomeAsUpEnabled(true);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (item.getItemId() == android.R.id.home) {
            onBackPressed();
        }
        return super.onOptionsItemSelected(item);
    }
}
